package com.jxs.android.weiget;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.jxs.android.ges.R;

public class FilterViewContent_Common extends LinearLayout {

	private Button okBtn, cancelBtn;
	private BtnOnClickListern btnOnClickListern;

	private Button time_index, customer_index, manager_index;

	private List<Button> indexs = new ArrayList<Button>();

	private LinearLayout time_bar, customer_bar, manage_bar;
	private RadioGroup filter_time_rg;

	private String conditionManagerId = "";//
	private String conditionCustomerId = "";//
	private String conditionTimeTag = "";// 时间

	private RadioButton filter_time_this_week;

	private ListView customer_listView, manager_listView;
	private Context context;

	private TextView custerSelectTv, managerSelectedTv;

	public FilterViewContent_Common(Context context) {
		this(context, null);
	}

	public FilterViewContent_Common(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		LayoutInflater.from(context).inflate(R.layout.bu_filer_common_view, this);
		init();
	}

	private void init() {
		initListView();
		initAlertDailogBtn();
		initFilterBar();// 过滤器bar
		initFilterIndex();// 检索过滤器
		initFilterRadioGroup();
		initTextView();
	}

	public void hindCustomerIndex() {
		customer_index.setVisibility(GONE);
	}

	private void initTextView() {
		custerSelectTv = (TextView) findViewById(R.id.customer_selected_tv);
		managerSelectedTv = (TextView) findViewById(R.id.manager_selected_tv);
	}

	public void setCustomerSelected(String text) {
		custerSelectTv.setText(text);
	}

	public void setManagerSelected(String text) {
		managerSelectedTv.setText(text);
	}

	private void initListView() {
		customer_listView = (ListView) findViewById(R.id.filter_customer_listview);
		manager_listView = (ListView) findViewById(R.id.filter_customer_manager_listview);
	}

	public void setCustomerData(final List<Map<String, String>> list, final ItemSelectedListener listener) {
		SimpleAdapter adapter = new SimpleAdapter(context, list, R.layout.bu_filter_common_row, new String[] { "content" }, new int[] { R.id.row_content });
		customer_listView.setAdapter(adapter);
		customer_listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Map<String, String> items = list.get(position);
				listener.logic(items.get("id"), items.get("content"));
			}

		});
	}

	public void setManagerData(final List<Map<String, String>> list, final ItemSelectedListener listener) {
		SimpleAdapter adapter = new SimpleAdapter(context, list, R.layout.bu_filter_common_row, new String[] { "content" }, new int[] { R.id.row_content });
		manager_listView.setAdapter(adapter);
		manager_listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Map<String, String> items = list.get(position);
				listener.logic(items.get("id"), items.get("content"));
			}

		});
	}

	static public interface ItemSelectedListener {
		public void logic(String id, String content);
	}

	private void initFilterIndex() {
		time_index = (Button) findViewById(R.id.filter_time_index);
		customer_index = (Button) findViewById(R.id.filter_customer_index);
		manager_index = (Button) findViewById(R.id.filter_manager_index);

		time_index.setOnClickListener(indexBarListener);
		customer_index.setOnClickListener(indexBarListener);
		manager_index.setOnClickListener(indexBarListener);

		indexs.add(time_index);
		indexs.add(customer_index);
		indexs.add(manager_index);

		for (Button btn : indexs) {
			if (btn.getId() == time_index.getId()) {
				btn.setSelected(true);
			} else {
				btn.setSelected(false);
			}
		}

	}

	private Button.OnClickListener indexBarListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			if (v.getId() == time_index.getId()) {
				time_bar.setVisibility(View.VISIBLE);
				customer_bar.setVisibility(View.GONE);
				manage_bar.setVisibility(View.GONE);
			} else if (v.getId() == customer_index.getId()) {
				time_bar.setVisibility(View.GONE);
				customer_bar.setVisibility(View.VISIBLE);
				manage_bar.setVisibility(View.GONE);
			} else if (v.getId() == manager_index.getId()) {
				time_bar.setVisibility(View.GONE);
				customer_bar.setVisibility(View.GONE);
				manage_bar.setVisibility(View.VISIBLE);
			}

			for (Button btn : indexs) {
				if (btn.getId() == v.getId()) {
					btn.setSelected(true);
				} else {
					btn.setSelected(false);
				}
			}

		}
	};

	private void initFilterBar() {
		time_bar = (LinearLayout) findViewById(R.id.filter_time_ll);
		customer_bar = (LinearLayout) findViewById(R.id.filter_customer_ll);
		manage_bar = (LinearLayout) findViewById(R.id.filter_customer_manager_ll);

		time_bar.setVisibility(View.VISIBLE);
		customer_bar.setVisibility(View.GONE);
		manage_bar.setVisibility(View.GONE);
	}

	private void initFilterRadioGroup() {
		filter_time_rg = (RadioGroup) findViewById(R.id.filter_time_rg);
		filter_time_this_week = (RadioButton) findViewById(R.id.filter_time_this_week);
		filter_time_this_week.setChecked(true);

		filter_time_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
				Object v = radioGroup.findViewById(checkedId).getTag();
				conditionTimeTag = null == v ? "" : v.toString();
			}
		});
	}

	private void initAlertDailogBtn() {
		okBtn = (Button) findViewById(R.id.filter_ok_btn);
		cancelBtn = (Button) findViewById(R.id.filter_cancel_btn);
	}

	public void setBtnOnClickListern(BtnOnClickListern btnListern) {
		this.btnOnClickListern = btnListern;
		okBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View paramView) {
				btnOnClickListern.okLogic();
			}
		});
		cancelBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View paramView) {
				btnOnClickListern.cancelLogic();
			}
		});
	}

	public String getConditionManagerId() {
		return conditionManagerId;
	}

	public void setConditionManagerId(String conditionManagerId) {
		this.conditionManagerId = conditionManagerId;
	}

	public String getConditionCustomerId() {
		return conditionCustomerId;
	}

	public void setConditionCustomerId(String conditionCustomerId) {
		this.conditionCustomerId = conditionCustomerId;
	}

	public RadioButton getConditionTime_All() {
		return filter_time_this_week;
	}

	public void setConditionTime_All(RadioButton conditionTime_All) {
		this.filter_time_this_week = conditionTime_All;
	}

	public String getConditionTimeTag() {
		return conditionTimeTag;
	}

	public void setConditionTimeTag(String conditionTimeTag) {
		this.conditionTimeTag = conditionTimeTag;
	}

	static public interface BtnOnClickListern {
		public void okLogic();

		public void cancelLogic();
	}

}
